﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class topCompany : BasePage
    {
        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            var list = from c in contex.top_companies orderby c.ID descending select c;
            var query = list.AsQueryable();


            List<top_company> coms = query.ToList();

            AspNetPager1.RecordCount = coms.Count;

            psd.DataSource = coms;
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptCategory.DataSource = psd;
            this.rptCategory.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
                int category_id = Convert.ToInt32(e.CommandArgument);
                top_company cat = contex.top_companies.SingleOrDefault(p => p.ID == category_id);
                if (cat != null)
                {
                    contex.top_companies.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                }
                BindGrid();
            }
        }

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                Label recordNo = e.Item.FindControl("lblNo") as Label;
                LinkButton button = e.Item.FindControl("btnDelete") as LinkButton;
                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                button.Attributes.Add("onclick", "return confirm('您确定要删除该公司记录吗');");
            }

        }
    }
}